package com.sgcc.sx.pmos.e_sx_home.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.common.base.BaseRecyclerAdapter;
import com.sgcc.sx.pmos.e_sx_home.bean.AddressBean;

import java.util.ArrayList;
import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.ViewHolder> {
    private List<AddressBean.DataBean.ObjListBean> mlist = new ArrayList<>();
    private BaseRecyclerAdapter.OnItemClickListener listener;

    public void setListener(BaseRecyclerAdapter.OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setMlist(List<AddressBean.DataBean.ObjListBean> mlist) {
        this.mlist = mlist;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ai_address, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.rv.setLayoutManager(new GridLayoutManager(holder.itemView.getContext(), 3));
        holder.rv.setAdapter(new AiAddressAdapterInner(mlist.get(position).getProvinces(), listener));

        if ("北京".equals(mlist.get(position).getAreaName())) {
            holder.tvTitle.setText("总部");
        } else {
            holder.tvTitle.setText(mlist.get(position).getAreaName());
        }
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvTitle;
        public RecyclerView rv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_area);
            rv = itemView.findViewById(R.id.rv);
        }
    }


    public static class AiAddressAdapterInner extends RecyclerView.Adapter<AiAddressAdapterInner.ViewHolder> {
        private List<AddressBean.DataBean.ObjListBean.ProvincesBean> innerList = new ArrayList<>();
        private BaseRecyclerAdapter.OnItemClickListener listener;

        public AiAddressAdapterInner(List<AddressBean.DataBean.ObjListBean.ProvincesBean> innerList, BaseRecyclerAdapter.OnItemClickListener listener) {
            this.innerList = innerList;
            this.listener = listener;

        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_address_inner, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
            String netProvinceName = innerList.get(position).getNetProvinceName();
            if (netProvinceName.contains("电力交易中心")) {
                netProvinceName = netProvinceName.replace("电力交易中心", "");
            }
            holder.tvTitle.setText(netProvinceName);
            holder.tvTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(position, innerList.get(position));
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return innerList.size();
        }

        public static class ViewHolder extends RecyclerView.ViewHolder {
            public TextView tvTitle;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                tvTitle = itemView.findViewById(R.id.tv_location);
            }
        }
    }
}
